package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    /**
     * 分页查询房间列表
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<RoomItemVo> pageRoomItemVo(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemVo(page, queryVo);
    }

    /**
     * 根据公寓id分页查询房间列表
     * @param page
     * @param id
     * @return
     */
    @Override
    public IPage<RoomItemVo> pageRoomItemVoById(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.pageRoomItemVoById(page, id);
    }

    /**
     * 根据id获取房间的详细信息
     * @param id
     * @return
     */
    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        //获取房间基础信息(roomInfo)
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        if (roomInfo == null) return null;
        //获取房间所属公寓信息(apartmentItemVo)
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentItemVoById(roomInfo.getApartmentId());
        //获取房间图片列表(graphVoList)
        List<GraphVo> graphVoList = graphInfoMapper.selectByItemTypeAndId(ItemType.ROOM, id);
        //获取属性信息列表(attrValueVoList)
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);
        //获取配套信息列表(facilityInfoList)
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectApartmentFacililtyList(id);
        //获取标签列表(labelInfoList)
        List<LabelInfo> labelInfoList = labelInfoMapper.selectApartmentLabelList(id);
        //获取支付方式列表(paymentTypeList)
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
        //获取杂费列表(feeValueVoList)
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(roomInfo.getApartmentId());
        //获取租期列表(leaseTermList)
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
        //获取房间是否被删除(isDelete)
        Boolean isDelete = roomInfo.getIsDeleted()==1;
        //获取房间是否已入住(isCheckIn)
        LambdaQueryWrapper<LeaseAgreement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LeaseAgreement::getRoomId, roomInfo.getId())
                    .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        Boolean isCheckIn = leaseAgreementMapper.selectCount(queryWrapper) > 0;
        //获取信息存入实体类中
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsDelete(isDelete);
        roomDetailVo.setIsCheckIn(isCheckIn);
        return roomDetailVo;
    }
}




